package com.guigu.qingcheng.controller;

import com.guigu.qingcheng.mapper.goods.GoodsTwo;
import com.guigu.qingcheng.pojo.TbAd;
import com.guigu.qingcheng.pojo.TbCategory;
import com.guigu.qingcheng.pojo.TbOrderItem;
import com.guigu.qingcheng.service.AdServiceFeign;
import com.guigu.qingcheng.service.GoodsServiceFeign;
import com.guigu.qingcheng.service.OrderServiceFeign;
import com.guigu.qingcheng.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;


@Controller
public class IndexController {

    @Autowired
    private AdServiceFeign adServiceFeign;
    @Autowired
    private GoodsServiceFeign goodsServiceFeign;
    @Autowired
    private OrderServiceFeign orderServiceFeign;

    @RequestMapping({"/","/index"})
   public String index(Model model) {
        //广告轮播
        R index_lb = adServiceFeign.findByPosition("index_lb");
        Map<String, Object> data = index_lb.getData();
        List<TbAd> list = (List<TbAd>) data.get("lbtList");
        model.addAttribute("lbt", list);

        //一二三级分类
        R tree = goodsServiceFeign.findTree();
        Map<String, Object> data1 = tree.getData();
        List<TbCategory> categoryList = (List<TbCategory>) data1.get("categoryTree");
        model.addAttribute("categoryList",categoryList);

        //今日推荐
        R todayPush = goodsServiceFeign.Today();
        Map<String, Object> data2 = todayPush.getData();
        List<GoodsTwo> todayPush1 = (List<GoodsTwo>) data2.get("todayPush");
        model.addAttribute("todayPush1",todayPush1);

        //商品热卖
        R host = orderServiceFeign.findHost();
        Map<String, Object> data3 = host.getData();
        List<TbOrderItem> host1 = (List<TbOrderItem>) data3.get("host");
        model.addAttribute("host1",host1);

        //特价商品
        R  price= orderServiceFeign.lessPrice();
        Map<String, Object> data4 = price.getData();
        List<TbOrderItem> lessprice = (List<TbOrderItem>) data4.get("lessprice");
        model.addAttribute("lessprice",lessprice);

        //新品排行
        R  commodities= orderServiceFeign.commodities();
        Map<String, Object> data5 = commodities.getData();
        List<TbOrderItem> ceqs= (List<TbOrderItem>) data5.get("commoditieseqs");
        model.addAttribute("ceqs",ceqs);

        return "index";
   }

    @RequestMapping("/positionAmusing")
    @ResponseBody
    public List<TbAd> positionAmusing(String position) {
        R index_lb = adServiceFeign.findByPosition(position);
        Map<String, Object> data = index_lb.getData();
        List<TbAd> list = (List<TbAd>) data.get("lbtList");
        return list;
    }
}
